package com.quick.calcu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quick.calcu.entity.Parent;
import com.quick.calcu.entity.Student;
import com.quick.calcu.entity.User;
import com.quick.calcu.mapper.ParentMapper;
import com.quick.calcu.service.ParentService;
import com.quick.calcu.service.StudentService;
import com.quick.calcu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ParentServiceImpl extends ServiceImpl<ParentMapper, Parent> implements ParentService {

    @Autowired
    private UserService userService;

    @Autowired
    private StudentService studentService;

    @Override
    public Parent getByUserId(Long userId) {
        LambdaQueryWrapper<Parent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Parent::getUserId, userId);
        return getOne(wrapper);
    }

    @Override
    public Parent getByStudentId(Long studentId) {
        LambdaQueryWrapper<Parent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Parent::getStudentId, studentId);
        return getOne(wrapper);
    }

    @Override
    public Parent getWithDetails(Long parentId) {
        // 查询家长基本信息
        Parent parent = getById(parentId);
        if (parent == null) {
            return null;
        }

        // 关联用户信息
        User user = userService.getById(parent.getUserId());
        parent.setUser(user);

        // 关联学生信息
        Student student = studentService.getById(parent.getStudentId());
        parent.setStudent(student);

        return parent;
    }
}